package jihe.collection;

import org.junit.Test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.TreeSet;

/**
 * Created by leiylong on 2020/5/30
 */
public class Set {
    /**
     * 底层数据结构是哈希表
     * 无序，唯一
     */
    @Test
    public void hashSet(){

        HashSet<Integer> hashSet=new HashSet<Integer>();
        hashSet.add(12);
        hashSet.add(13);
        hashSet.add(12);

        Iterator iterator=hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    /**
     * 底层数据结构是哈希表和链表
     * 有序，唯一
     */
    @Test
    public void linkedHashSet(){
        LinkedHashSet linkedHashSet=new LinkedHashSet();

        linkedHashSet.add("new");
        linkedHashSet.add("new1");
        linkedHashSet.add("new");
        Iterator iterator=linkedHashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    /**
     * 有序，唯一
     *
     */
   @Test
    public void treeSet(){
       TreeSet treeSet=new TreeSet();

       treeSet.add("new");
       treeSet.add("new1");
       treeSet.add("new");

       Iterator iterator=treeSet.iterator();
       while (iterator.hasNext()){
           System.out.println(iterator.next());
       }
    }


}
